package com.ibeeking.found.msg.rest.service.util;


import com.ibeeking.found.msg.service.common.bo.MessageInfoBO;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.builder.outxml.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ReplyMessageUtils
 * @Description 回复消息工具类
 * @Author ibeeking
 * @Date 2021-02-06 18:54
 **/
public class ReplyMessageUtils {

    public static WxMpXmlOutMessage replyMessage(WxMpXmlMessage wxMessage, List<MessageInfoBO> msgs) {
        WxMpXmlOutMessage out = null;
        if (msgs != null && msgs.size() > 0) {
            MessageInfoBO messageInfo = msgs.get(0);
            if (WxConsts.XmlMsgType.TEXT.equals(messageInfo.getMsgType())) {
                //文本
                out = new TextBuilder().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).content(messageInfo.getContent()).build();
            }
            if (WxConsts.XmlMsgType.IMAGE.equals(messageInfo.getMsgType())) {
                //图片
                out = new ImageBuilder().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).mediaId(messageInfo.getMediaId()).build();
            }
            if (WxConsts.XmlMsgType.VOICE.equals(messageInfo.getMsgType())) {
                //语音
                out = new VoiceBuilder().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).mediaId(messageInfo.getMediaId()).build();
            }
            if (WxConsts.XmlMsgType.VIDEO.equals(messageInfo.getMsgType())) {
                out = new VideoBuilder().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).mediaId(messageInfo.getMediaId())
                        .title(messageInfo.getTitle()).description(messageInfo.getDescription()).build();
            }
            if (WxConsts.XmlMsgType.MUSIC.equals(messageInfo.getMsgType())) {
                out = new MusicBuilder().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
                        .thumbMediaId(messageInfo.getMediaId())
                        .title(messageInfo.getTitle()).description(messageInfo.getDescription())
                        .musicUrl(messageInfo.getUrl()).hqMusicUrl(messageInfo.getHqUrl()).build();
            }
            if (WxConsts.KefuMsgType.NEWS.equals(messageInfo.getMsgType())) {
                List<WxMpXmlOutNewsMessage.Item> list = new ArrayList<>();
                WxMpXmlOutNewsMessage.Item t;
                for (WxMpXmlOutNewsMessage.Item item : messageInfo.getArticles()) {
                    t = new WxMpXmlOutNewsMessage.Item();
                    t.setTitle(item.getTitle());
                    t.setDescription(item.getDescription());
                    t.setPicUrl(item.getPicUrl());
                    t.setUrl(item.getUrl());
                    list.add(t);
                }
                out = new NewsBuilder().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).articles(list).build();
            }
        }
        return out;
    }
}
